package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.ZhwyBuilding;
import com.ruoyi.system.domain.ZhwyDowntown;
import com.ruoyi.system.mapper.ZhwyBuildingMapper;
import com.ruoyi.system.mapper.ZhwyDowntownMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ZhwyShopsMapper;
import com.ruoyi.system.domain.ZhwyShops;
import com.ruoyi.system.service.IZhwyShopsService;

/**
 * 商品信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-07
 */
@Service
public class ZhwyShopsServiceImpl implements IZhwyShopsService 
{
    @Autowired
    private ZhwyShopsMapper zhwyShopsMapper;
    @Autowired
    private ZhwyBuildingMapper zhwyBuildingMapper;
    @Autowired
    private ZhwyDowntownMapper zhwyDowntownMapper;
    @Autowired
    private com.ruoyi.mapper.ZhwyHirerMapper zhwyHirerMapper;


    public Object findByBuildingId(Long id) {
        List<Long> floor = zhwyShopsMapper.findFloor(id);
//        List<Long> floor1 = floor.stream().distinct().collect(Collector.toList());//商铺层
        ZhwyShops zhwyShops = new ZhwyShops();
        zhwyShops.setBuildingId(id);
        zhwyShops.setDeleted(0L);
        //在某个楼栋（第几栋楼）中存在的商铺信息
        List<ZhwyShops> zhwyShops1 = zhwyShopsMapper.selectZhwyShopsList(zhwyShops);
        Map<Long, List<ZhwyShops>> map = zhwyShops1.stream().collect(Collectors.groupingBy(ZhwyShops::getFloor));

        Map<String,Object> map1 = new HashMap<>();//创建map集合返回前端
        List<Long> ceng = new ArrayList<>();//创建list集合存储 楼层数据
        List<Object> sp = new ArrayList<>();//创建list集合存储 每个楼层存在的店铺
        for (Long key : map.keySet()) {//遍历map集合，对数据进行处理，key楼层,value 商铺
            ceng.add(key);
            sp.add(map.get(key));
        }
        map1.put("ceng",ceng);
        map1.put("sp",sp);
        return map1;
    }

    /**
     * 查询商品信息
     * 
     * @param id 商品信息主键
     * @return 商品信息
     */
    @Override
    public ZhwyShops selectZhwyShopsById(Long id)
    {

//        return zhwyShopsMapper.selectZhwyShopsById(id);
        com.ruoyi.domain.ZhwyHirer zhwyHirer = new com.ruoyi.domain.ZhwyHirer();
        ZhwyShops zhwyShops = zhwyShopsMapper.selectZhwyShopsById(id);
        if(zhwyShops!=null){
            ZhwyBuilding zhwyBuilding = zhwyBuildingMapper.selectZhwyBuildingById(zhwyShops.getBuildingId());
            ZhwyDowntown zhwyDowntown = zhwyDowntownMapper.selectZhwyDowntownById(zhwyBuilding.getDowntownId());
            zhwyHirer.setHaveCount(zhwyShops.getId());
            System.out.println(zhwyHirer.getHaveCount());
            List<com.ruoyi.domain.ZhwyHirer> zhwyHirers = zhwyHirerMapper.selectZhwyHirerList(zhwyHirer);
            System.out.println(zhwyHirers);
            for (com.ruoyi.domain.ZhwyHirer zhwyHirer1 : zhwyHirers){
                if(zhwyHirer1.getState()==0){
                    zhwyShops.setHireInfo(zhwyHirer1.getCompany());
                }
            }
            zhwyShops.setDowntownName(zhwyDowntown.getName());
            zhwyShops.setbName(zhwyBuilding.getName());
        }
        return zhwyShops;
    }

    /**
     * 查询商品信息列表
     * 
     * @param zhwyShops 商品信息
     * @return 商品信息
     */
    @Override
    public List<ZhwyShops> selectZhwyShopsList(ZhwyShops zhwyShops)
    {
        return zhwyShopsMapper.selectZhwyShopsList(zhwyShops);
    }

    /**
     * 新增商品信息
     * 
     * @param zhwyShops 商品信息
     * @return 结果
     */
    @Override
    public int insertZhwyShops(ZhwyShops zhwyShops)
    {
        zhwyShops.setCode(zhwyShops.getDowntownId()+"-"+zhwyShops.getBuildingId()+"-"+zhwyShops.getFloor()+"-"+zhwyShops.getRoom());
        return zhwyShopsMapper.insertZhwyShops(zhwyShops);
    }

    /**
     * 修改商品信息
     * 
     * @param zhwyShops 商品信息
     * @return 结果
     */
    @Override
    public int updateZhwyShops(ZhwyShops zhwyShops)
    {
        zhwyShops.setCode(zhwyShops.getDowntownId()+"-"+zhwyShops.getBuildingId()+"-"+zhwyShops.getFloor()+"-"+zhwyShops.getRoom());
        return zhwyShopsMapper.updateZhwyShops(zhwyShops);
    }

    /**
     * 批量删除商品信息
     * 
     * @param ids 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    public int deleteZhwyShopsByIds(Long[] ids)
    {
        return zhwyShopsMapper.deleteZhwyShopsByIds(ids);
    }

    /**
     * 删除商品信息信息
     * 
     * @param id 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteZhwyShopsById(Long id)
    {
        return zhwyShopsMapper.deleteZhwyShopsById(id);
    }
}
